#!/usr/bin/env bash
. ./scripts/devcontainer/_assert-in-container "$0" "$@"

set -euo pipefail


LOGS="${DARK_CONFIG_RUNDIR}/logs"
LOG="$LOGS/pub-sub-emulator.log"

grey="\033[1;30m"
reset="\033[0m"

echo -e "Running PubSub emulator ${grey}($LOG)${reset}"

# This is slow to start up, so if it's already loaded, don't restart
if [[ $(pgrep --full 'cloud-pubsub-emulator-.*.jar') == "" ]] ; then
  gcloud beta emulators pubsub start \
    --host-port=$PUBSUB_EMULATOR_HOST \
    > "$LOG" 2>&1 &
fi

# Pubsub takes ~1s to startup
while ! curl --output /dev/null --silent --head http://$PUBSUB_EMULATOR_HOST ; do
  sleep 0.3
  echo .
done

echo "Finished loading PubSub emulator"
